package cn.huanglei936.essay.thymeleaf.controller;

import cn.huanglei936.essay.thymeleaf.api.ucenter.Client;
import cn.huanglei936.essay.thymeleaf.mail.CustomMailServceImpl;
import cn.huanglei936.essay.thymeleaf.mail.MailTestEntiry;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.io.IOException;

/**
 * 首页
 *
 * @author huanglei
 * @email lhuang@matmass.com
 * @data 2018/9/7 13:27
 */
@RestController
@RequestMapping
public class IndexController {

    @Autowired
    private CustomMailServceImpl customMailServce;

    @GetMapping("/index1")
    public String index() {
        return "index";
    }

    @GetMapping("/addUser")
    public String addUser() {

        Client client = new Client();
        String s = client.uc_user_register("sync_user1", "ffffff", "sync_user1@qq.com");
        System.out.println("s = " + s);
        int uid = Integer.valueOf(s);
        String s2 = client.uc_get_user("sync_user1", uid);
        System.out.println("s2 = " + s2);
        return "addUser";
    }

    @GetMapping("/delUser")
    public String delUser() {

        Client client = new Client();
        String s2 = client.uc_user_delete("166");
        System.out.println("s2 = " + s2);
        return "getUser";
    }

    @GetMapping("/mailtest")
    public String mailtest() {

        String to = "hlmilies@163.com";
        MailTestEntiry mailTestEntiry = new MailTestEntiry();
        mailTestEntiry.setMessageCode("10001");
        mailTestEntiry.setMessageStatus("SUCCESS");
        mailTestEntiry.setCause("测试邮件");
        try {
            customMailServce.sendMail(to, mailTestEntiry, "测试邮件", "mail_test.ftl");
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }

        return "Send mail test......";
    }

    @GetMapping("/mailtest1")
    public String mailtest1() {

        customMailServce.sendMail1("hlmilies@163.com");
        return "Send mail test1......";
    }

}
